package com.bdc.whatwhat.session;

import android.content.Context;
import android.content.SharedPreferences;

import com.bdc.whatwhat.modele.MonFlux;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;


/**
 * Permet de gérer la sauvegarde de données dans la session
 * Fonctionne comme une MAP, système CLE / VALEUR
 */
public class SessionManager {

    private Context context; //le context de l'application

    //Les clés
    private static final String PREFS = "PREFS";
    private static final String STATUT = "STATUT";
    private static final String TIME = "TIME";
    private static final String PROFIL = "PROFIL";
    private static final String TOKEN = "TOKEN";
    private static final String JETON = "JETON";
    private static final String AMIS = "AMIS";
    private static final String MON_FLUX = "MON_FLUX";

    private static Whatter profil;

    public SessionManager(Context context){
        this.context = context;
    }

    public void setStatut(String statut){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(STATUT, statut);
        editor.commit();
    }
    public String getStatut(){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        return pref.getString(STATUT,null);
    }

    public void setTime(long time){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putLong(TIME, time);
        editor.commit();
    }

    public long getTime(){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        return pref.getLong(TIME,0l);
    }

    public void setProfil(Whatter profil){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(PROFIL, (new Gson()).toJson(profil));
        editor.commit();

        SessionManager.profil = profil;
    }

    public Whatter getProfil(){
        if(SessionManager.profil == null) {
            SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
            String profilJSON = pref.getString(PROFIL, null);
            if (profilJSON != null) {
                SessionManager.profil = (new Gson()).fromJson(profilJSON, Whatter.class);
                return profil;
            }
            else
                return null;
        }else
            return SessionManager.profil;
    }

    public void setToken(String token){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(TOKEN, token);
        editor.commit();
    }

    public String getToken(){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        return pref.getString(TOKEN,null);
    }

    public void setJeton(String jeton){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(JETON, jeton);
        editor.commit();
    }

    public String getJeton(){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        return pref.getString(JETON,null);
    }

    public void setAmis(List<Whatter> amis){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(AMIS, new Gson().toJson(amis));
        editor.commit();
    }

    public List<Whatter> getAmis(){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        String amisJSON = pref.getString(AMIS,null);
        if(amisJSON != null)
            return (new Gson()).fromJson(amisJSON,new TypeToken<List<Whatter>>(){}.getType());
        else
            return null;
    }

    public void setMonFlux(MonFlux monFlux){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(MON_FLUX, new Gson().toJson(monFlux));
        editor.commit();
    }

    public MonFlux getMonFlux(){
        SharedPreferences pref = context.getSharedPreferences(PREFS, 0);
        String monFluxJSON = pref.getString(MON_FLUX,null);
        if(monFluxJSON != null)
            return (new Gson()).fromJson(monFluxJSON,MonFlux.class);
        else
            return null;
    }
}
